$(function(){
    getOrderData();

    var orderItem = $("#orderItem");
    $("#pay-success").hide();
    $("#pay-failure").hide();
    $("#submitOrder").tap(submitOrderFn);
    $("#paySure").tap(paySureFn);
    $("#payClose").tap(function(){
        $("#pay").hide();
    });
    orderItem.on("tap",".news-minus",function(){
        var _this = $(this);
        var num = _this.parent().find("input").val();
        num--;
        if(num<1){
            num = 1;
        }
        _this.parent().find("input").val(num);
        partPriceFn(num)
    }).on("tap",".news-plus",function(){
        var _this = $(this);
        var num = _this.parent().find("input").val();
        num++;
        _this.parent().find("input").val(num);
        partPriceFn(num)
    });

    function partPriceFn(num){
        $("li").each(function(){
            var _vm = $(this)
            var onePrice = parseInt(_vm.find(".news-price").text().replace("￥","").replace(".00",""));
            partPrice = onePrice*num
            _vm.find(".partPrice").text(partPrice+".00");
            totalPrice();
        });
    }

    function paySureFn(){
        var status = 1;
        $("#pay-main").hide();
        $("li").each(function(){
            var _this = $(this);
            var id = _this.attr("data-id");
            var num = _this.parent().find("input").val();
            var partPrice = parseInt(_this.find("#partPrice").text());
            var totalPrice = parseInt($("#totalPrice").text());
            var option = {
                type: 'post',
                url: Api.confirmOrder,
                data: {
                    postType: 'orderSubmit',
                    id: id,
                    num:num,
                    partPrice:partPrice,
                    totalPrice:totalPrice
                }
            };
            requestEvent(option).then(function (res) {
                if(res.status){
                    status = 0
                }
            })
        })
        if(status == 1 ){
            $("#pay-success").show();
            alert("购买成功");
            initFn();
        }else{
            $("#pay-failure").show();
            alert("购买失败");
        }
    }

    function submitOrderFn(){
        var payPrice = $("#totalPrice").text();
        $("#pay").show();
        $("#payPrice").text("￥"+payPrice);
    }

    function totalPrice(){
        var sum = 0;
        $(".partPrice").each(function(){
            console.log($(this).text());
            sum += parseInt($(this).text());
        });
        $("#totalPrice").text(sum+".00");
    }

    function createProductHtml(data) {
        var str = '';
        data.forEach(function (item) {
            if(item.num == undefined){
                item.num = 1;
            }
            str += '<li data-id="'+item.id+'">'
                +'<img src="'+item.img_src+'" alt="confirm_order4.png">'
                +'<div class="news-right">'
                +'<span class="news-content">'+item.title+'</span>'
                +'<span>'+item.subtitle+'</span>'
                +'</div>'
                +'<div class="news-bottom">'
                +'<span class="news-price">￥'+item.price+".00"+'</span>'
                +'<div class="news-bottom-center">'
                +'<span class="news-minus">-</span>'
                +'<input type="text" name="amount" value="'+item.num+'" class="news-number">'
                +'<span class="news-plus">+</span>'
                +'</div>'
                +'</div>'
                +'<div class="mode">'
                +'<span>配送方式</span>'
                +'<span class="mode-act2"></span>'
                +'<span class="mode-act1">快递 免邮</span>'
                +'</div>'
                +'<div class="mode mode-1">'
                +'<span>运费险</span>'
                +'<span class="mode-act2"></span>'
                +'<span class="mode-act1 act1">未选择</span>'
                +'</div>'
                +'<div class="mode mode-1">'
                +'<span>买家留言:</span>'
                +'<span class="mode-act3">选填 填写内容已和卖家协商确认</span>'
                +'</div>'
                +'<div class="mode-2">'
                +'<span class="mode-act4 partAmount">共一件商品</span>'
                +'<span class="mode-act5">小计:<i>￥<em class="partPrice" id="partPrice">'+item.num*item.price+".00"+'</em></i></span>'
                +'</div>'
                +'</li>'
        });
        orderItem.html(str);
        totalPrice();
        initFn();
    }

    function initFn(){
        $("li").each(function(){
            var pro_id = $(this).attr("data-id");
            var option = {
                type: 'post',
                url: Api.confirmOrder,
                data: {
                    postType: 'init',
                    pro_id: pro_id
                }
            };
            requestEvent(option);
        });
    }

    function getOrderData(){
        var params = getParams();
        var conId = params.id;
        if (!conId) {
            var option = {
                url: Api.confirmOrder,
                data: {
                    getType: 'getOrder'
                }
            };
            requestEvent(option).then(function (data) {
                createProductHtml(data);
            })
        }else{
            confirmById()
        }

    }

    function confirmById() {
        var params = getParams();
        var conId = params.id;
        if (!conId) {
            return false;
        }
        var option = {
            url: Api.confirmOrder,
            data: {
                getType: 'confirmById',
                conId: conId
            }
        };
        requestEvent(option).then(function (data) {
            console.log(data)
            createProductHtml(data);
        })
    }

    function getParams() {
        var params = window.location.search;
        var obj = {};
        if (params) {
            params = params.replace('?', '').split('&');
            for (var i = 0, n = params.length; i < n; i++) {
                var key = params[i].split('=')[0];
                var val = params[i].split('=')[1];
                obj[key] = decodeURIComponent(val);
            }
        }
        return obj;
    }

});
